import json

import importlib


def ParseJsonToObj(jsonStr, pluginClass):
    parseData = json.loads(jsonStr.strip('\t\r\n'))
    pluginObj = pluginClass()
    pluginObj.__dict__ = parseData
    return pluginObj


def run(json_file):
    with open(json_file, 'r', encoding='utf8') as fp:
        job_json = json.load(fp)
    job_content = job_json["job_content"]
    works_array = job_content["works"]
    works_dict = {}
    current_works = ""
    for works in works_array:
        worker_id = works["worker_id"]
        worker_type = works["worker_type"]
        works_dict[worker_id] = works
        if worker_type == "input":
            current_works = works
            continue

    source_df = ""
    while True:
        if current_works == None:
            break
        worker_id = current_works["worker_id"]
        worker_type = current_works["worker_type"]
        para_json = current_works["para"]
        worker_s = importlib.import_module(worker_type + '.' + worker_id)

        if worker_type == "input":
            source_df = worker_s.run(para_json)
        if worker_type == "other":
            source_df = worker_s.run(source_df, para_json)
        if worker_type == "output":
            worker_s.run(source_df, para_json)
        if "next_step" not in current_works:
            break
        next_steps = current_works["next_step"]
        if len(next_steps) == 0:
            break
        current_works = works_dict[next_steps[0]]


if __name__ == '__main__':
    # run("doc/1.json")
    # run("doc/db_reader.json")
    # run("doc/mysql_mongo.json")
    run("doc/faker_parquet.json")
    # run("doc/fakers_parquet.json")
    # run("doc/parquet_console.json")
    # run("doc/file_console.json")
